package com.tnovoselec.android.common.ui.tabbar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.RadioButton;

import com.tnovoselec.android.common.R;


public class TabBarButton extends RadioButton {

	private final Context context;

	public TabBarButton(Context context) {
		super(context);
		this.context = context;
	}

	public TabBarButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public void setState(String label, int imageId, int width) {
		setState(label, imageId, width, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_YELLOW);
	}

	public void setState(String label, int imageId, int width, int offState, int onState) {
		RadioStateDrawable offDrawable = new RadioStateDrawable(context, imageId, label, false, width, offState);
		RadioStateDrawable onDrawable = new RadioStateDrawable(context, imageId, label, true, width, onState);
		setStateImageDrawables(onDrawable, offDrawable);
	}

	public void setState(int onId, int offId) {
		Resources resource = super.getResources();
		setStateImageDrawables(resource.getDrawable(offId), resource.getDrawable(onId));
	}

	private void setStateImageDrawables(Drawable onDrawable, Drawable offDrawable) {
		int stateChecked = android.R.attr.state_checked;
		int stateFocused = android.R.attr.state_focused;
		int statePressed = android.R.attr.state_pressed;
		int stateWindowFocused = android.R.attr.state_window_focused;

		Resources resource = super.getResources();
		Drawable xDrawable = resource.getDrawable(R.drawable.bottom_bar_highlight);

		StateListDrawable drawables = new StateListDrawable();
		drawables.addState(new int[] { stateChecked, -stateWindowFocused }, offDrawable);
		drawables.addState(new int[] { -stateChecked, -stateWindowFocused }, offDrawable);
		drawables.addState(new int[] { stateChecked, statePressed }, onDrawable);
		drawables.addState(new int[] { -stateChecked, statePressed }, onDrawable);
		drawables.addState(new int[] { stateChecked, stateFocused }, onDrawable);
		drawables.addState(new int[] { -stateChecked, stateFocused }, offDrawable);
		drawables.addState(new int[] { stateChecked }, onDrawable);
		drawables.addState(new int[] { -stateChecked }, offDrawable);
		drawables.addState(new int[] {}, xDrawable);

		this.setButtonDrawable(drawables);
	}
}
